package com.bitcat.firstapp.ui.activity

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.doAfterTextChanged
import com.bitcat.firstapp.app.db.DataBaseHelper
import com.bitcat.firstapp.databinding.ActivityRegisterBinding
import es.dmoral.toasty.Toasty

class RegisterActivity : AppCompatActivity() {

    private lateinit var binding: ActivityRegisterBinding

    private lateinit var dataBaseHelper: DataBaseHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityRegisterBinding.inflate(layoutInflater)
        setContentView(binding.root)
//        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
//            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
//            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
//            insets
//        }

        dataBaseHelper = DataBaseHelper(this)

        setupListeners()

        binding.registerBtn.setOnClickListener {
            register()
        }
    }


    private fun setupListeners() {
//        binding.usernameInput.addTextChangedListener {
//
//        }

        // 用户名输入框内容变化后，清除错误信息
        binding.usernameInput.doAfterTextChanged {
            if (!it.isNullOrEmpty() && binding.usernameLayout.error != null) {
                binding.usernameLayout.error = null
            }
        }
        // 密码输入框内容变化后，清除错误信息
        binding.passwordInput.doAfterTextChanged {
            if (!it.isNullOrEmpty() && binding.passwordLayout.error != null) {
                binding.passwordLayout.error = null
            }
        }
        // 确认密码输入框内容变化后，清除错误信息
        binding.passwordAgainInput.doAfterTextChanged {
            if (!it.isNullOrEmpty() && binding.passwordAgainLayout.error != null) {
                binding.passwordAgainLayout.error = null
            }
        }
    }


    //注册功能
    private fun register() {
        //1. 获取输入框的内容
        val username = binding.usernameInput.text.toString()
        val password = binding.passwordInput.text.toString()
        val passwordConfirm = binding.passwordAgainInput.text.toString()

        //2. 非空校验
        when {
            username.isEmpty() -> {
                binding.usernameLayout.error = "用户名不能为空"
            }

            password.isEmpty() -> {
                binding.passwordLayout.error = "密码不能为空"
            }

            passwordConfirm.isEmpty() -> {
                binding.passwordAgainLayout.error = "请再次输入密码"
            }

            password != passwordConfirm -> {
                binding.passwordAgainLayout.error = "两次密码不一致"
            }
            //密码必须在6-12之间
            password.length !in 6..12 -> {
                binding.passwordLayout.error = "密码必须在6-12位之间"
                binding.passwordAgainLayout.error = "密码必须在6-12位之间"
            }
            //检测用户是否已经注册
            dataBaseHelper.checkUserExist(username) -> {
                Toasty.error(this, "用户名已经存在", Toasty.LENGTH_SHORT).show()
            }

            else -> {
                val result = dataBaseHelper.registerUser(username, password)
                if (result) {
                    Toasty.success(this, "注册成功", Toasty.LENGTH_SHORT).show()
                    startActivity(Intent(this, LoginActivity::class.java))
                    finish() //销毁当前页面
                } else {
                    Toasty.error(this, "注册失败", Toasty.LENGTH_SHORT).show()
                }
            }
        }
    }
}